package com.adaxi.adc.business.vehicletrip.service.impl;

import com.adaxi.adc.business.vehicletrip.mapper.VehicleTripMapper;
import com.adaxi.adc.business.vehicletrip.pojo.VehicleTrip;
import com.adaxi.adc.business.vehicletrip.service.VehicleTripService;
import com.adaxi.adc.business.vehicletrip.vo.MyTripVo;
import com.adaxi.adc.business.vehicletrip.vo.VehicleTripVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 钟红树
 */
@Service
public class VehicleTripServiceImpl extends ServiceImpl<VehicleTripMapper, VehicleTrip>
        implements VehicleTripService {

    final
    VehicleTripMapper mapper;

    public VehicleTripServiceImpl(VehicleTripMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void insert(VehicleTripVo vo) {
        mapper.insert(vo);
    }

    @Override
    public List<MyTripVo> getMyTrip(Long id) {
        List<MyTripVo> list = mapper.getMyTrip(id);
        return list;
    }

    @Override
    public void updateStatusById(String id, String status) {
        mapper.updateStatusById(id, status);
    }

    @Override
    public void increasePassengers(String id, int numToAdd) {
        // 1. 获取当前的 VehicleTrip
        VehicleTrip currentTrip = getById(id);
        if (currentTrip == null) {
            throw new IllegalArgumentException("未找到 ID 为：的 VehicleTrip " + id);
        }

        // 2. 增加乘客数量
        int newNumPassengers = currentTrip.getNum_passengers() + numToAdd;
        currentTrip.setNum_passengers(newNumPassengers);

        // 3. 更新 VehicleTrip
        updateById(currentTrip);
    }

    @Override
    public Long getVehicleIdByTripId(String tripId) {
        VehicleTrip trip = getById(tripId);
        if (trip != null) {
            return trip.getVehicle_id();
        } else {
            throw new RuntimeException("未找到指定的行程 ID:" + tripId);
        }
    }

    @Override
    public void cancelTrip(String id) {
        mapper.deleteById(id);
    }


}




